﻿#encoding: utf-8
#those text file will be printed to pdf printer and send to printer by powershell scripts

require 'mysql2'
require 'yaml'
require 'digest/md5'
require 'net/http'
require 'rest-client'
require 'json'
require 'date'
require 'time'
load 'router.rb'
load 'get_orders.rb'
load 'wechat_api.rb'
load 'split-orders-lib-tt.rb'
load 'tutem_utils.rb'

@rds = @rds_tt

#re-calcaluated all first_item and item_counts for all existed recs
def gen_first_item
    sqlu = "select * from ogoods.pospal_orders"
    res = @rds.query(sqlu)
    res.each do |r|
        oid=r['order_id']

        if r['raw_data'].nil?
            sqlu = "UPDATE ogoods.pospal_orders set first_item='', items_count=0 where order_id='#{oid}'"
        else
            order = JSON.parse(r['raw_data'])
            items_count = 0
            if !order['items'].nil?
                order['items'].each do |itm|
                    items_count += itm['productQuantity'].to_i
                end
            end
            first_item = ''
            first_item = order['items'][0]['productName'] if !order['items'].nil? && !order['items'].empty?
            sqlu = "UPDATE ogoods.pospal_orders set first_item='#{first_item}', items_count=#{items_count} where order_id='#{oid}'"
        end
        resu = @rds.query(sqlu)
    end
end

#fix raw_data error
def fix_raw_data
    sqlu = "select order_id, raw_data from ogoods.pospal_orders where raw_data like '\"{%'"
    res = @rds.query(sqlu)
    res.each do |r|
        oid=r['order_id']
        raw_data = r['raw_data']
        raw_data = raw_data[1..raw_data.length-2] if raw_data[0..1]=='"{'
        sqlu = "UPDATE ogoods.pospal_orders set raw_data='#{raw_data}' where order_id='#{oid}'"
        resu = @rds.query(sqlu)
    end
end

#re-generate statement for order since 2019-07-12
def gen_statement
    sql = "select * from ogoods.pospal_orders where line!='[X]' and order_time >= '2019-07-12'"
    res = @rds.query(sql)
    res.each do |r|
        rationalize_order r
    end
end

#fix 2023-05-28 修复哪些支付成功了但是没有被系统设置paid=1以及支付时间未设置的订单
def fix_230528_paid_but_no_record

    sql = "select id, uid, add_time, order_id, order_code from tutem_mall.eb_store_order where paid = 0 and pay_type = 'weixin' order by add_time desc limit 100"
    #trade_no = 'wx1685223860555982O2'

    (@rds.query(sql)).each do  |q|

        order_id = q['id']
        trade_no = q['order_id']
        add_time = q['add_time']
        order_code = q['order_code'].to_s
        uid = q['uid']

        #next if order_id != 11541 #测试单条处理

        pay_time = queryWechatTradePaytime trade_no
        if pay_time > 0
            created_at = Time.at(add_time).strftime('%Y-%m-%d %H:%M:%S')
            paid_at = Time.at(pay_time).strftime('%Y-%m-%d %H:%M:%S')
            puts "[order##{sprintf'% 5d',order_id}] uid: #{sprintf'% 7d',uid} trade_no: #{trade_no} created_at: #{created_at} paid_at: #{paid_at} order_code: #{order_code}"
            usql = "update tutem_mall.eb_store_order set paid = 1, pay_time = #{pay_time} where id = #{order_id}"
            res = queryRds usql
            new_orders = split_and_register_order_id order_id #只处理订单order_code依旧为空的
        end

    end
end

def show_missed_payment

    sql = "select id, uid, add_time, order_id, order_code from tutem_mall.eb_store_order where paid = 0 and pay_type = 'weixin' order by add_time desc limit 1100"

    (@rds.query(sql)).each do  |q|

        order_id = q['id']
        trade_no = q['order_id']
        add_time = q['add_time']
        order_code = q['order_code'].to_s
        uid = q['uid']

        pay_time = queryWechatTradePaytime trade_no
        if pay_time > 0
            created_at = Time.at(add_time).strftime('%Y-%m-%d %H:%M:%S')
            paid_at = Time.at(pay_time).strftime('%Y-%m-%d %H:%M:%S')
            puts "[order##{sprintf'% 5d',order_id}] uid: #{sprintf'% 7d',uid} trade_no: #{trade_no} created_at: #{created_at} paid_at: #{paid_at} order_code: #{order_code}"
        end

    end
end
#show_missed_payment

#应该还有一些设置了支付成功但实际没有pickup_book记录，或者pcode_info为NULL的, 无论状态如何，只要没有pickup记录，就可以处理
def fix_230528_pickup_missed

    sql = "select * from tutem_mall.eb_store_order where paid = 1 and id not in (select oid from tutem_mall.eb_pickup_book)"
    order_ids = []
    (queryRds(sql)).each do |q|
        order_id = q['id']
        trade_no = q['order_id']
        add_time = q['add_time']
        pay_time = q['pay_time']
        order_code = q['order_code'].to_s
        uid = q['uid']
        created_at = Time.at(add_time).strftime('%Y-%m-%d %H:%M:%S')
        paid_at = Time.at(pay_time).strftime('%Y-%m-%d %H:%M:%S')
        puts "[order##{sprintf'% 5d',order_id}] uid: #{sprintf'% 7d',uid} trade_no: #{trade_no} created_at: #{created_at} paid_at: #{paid_at} order_code: #{order_code}"
        new_orders = split_and_register_order_id order_id #只处理订单order_code依旧为空的
    end
    puts "no pickup_books: #{order_ids.to_s}"

end
#fix_230528_pickup_missed

#tutem_utils.rb中的用户当为spreader且spread_uid不为零的时候，之前的代表标签打得不对
def fix_240409_rep

    sql = "select * from tutem_mall.eb_user where spread_uid != 0 and is_promoter > 0"
    count = 0
    (queryRds(sql)).each do |q|
        uid = q['uid']
        name = q['real_name']
        agency = q['agency']
        syncUserData uid
    end

end
#fix_240409_rep
